package xcmg.device.task;

import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xcmg.device.service.order.GpsMmJisoPoHeadService;
import xcmg.device.service.order.GpsMmSwOrderLabelService;
import xcmg.device.service.order.GpsMmSwOrderService;
import xcmg.device.service.task.SyncGpsTaskService;
import xcmg.device.service.task.SyncMmPartSupplierTaskService;
import xcmg.device.util.CommonUtils;

import javax.annotation.Resource;
import java.util.Date;

@Component
@Slf4j
public class SyncGpsTask {

    @Resource
    private SyncGpsTaskService syncGpsTaskService;

    @Resource
    private SyncMmPartSupplierTaskService syncMmPartSupplierTaskService;

    @Autowired
    private GpsMmSwOrderService gpsMmSwOrderService;

    @Autowired
    private GpsMmSwOrderLabelService gpsMmSwOrderLabelService;

    @Autowired
    private GpsMmJisoPoHeadService gpsMmJisoPoHeadService;

    /**
     * 每5分钟同步一次gps信息
     */
//    @Scheduled(cron = "0 0/5 * * * ?")
//    @SchedulerLock(name = "syncGpsPartInfo", lockAtMostFor = 5*60*1000, lockAtLeastFor = 5*60*1000)
    public void syncGpsPartInfo(String s) {
        int second = CommonUtils.tryParseInt(s, 0);
        Date t = null;
        if (second > 0) {
            t = DateTime.now().minusSeconds(second).toDate();
        }
        syncGpsTaskService.syncGpsPart(t);
    }


    //    @Scheduled(cron = "0 0/8 * * * ?")
//    @SchedulerLock(name = "syncGpsPartSupplier", lockAtMostFor = 5 * 60 * 1000, lockAtLeastFor = 5 * 60 * 1000)
    public void syncGpsPartSupplier(String s) {
        int second = CommonUtils.tryParseInt(s, 10);
        Date t = DateTime.now().minusSeconds(second).toDate();
        syncMmPartSupplierTaskService.syncGpsPartSupplier(t, null, null);
    }

    public void syncAllTimeGpsPartSupplierHandler(String s) {
        syncMmPartSupplierTaskService.syncGpsPartSupplier(null, s, null);
    }

    public void syncGpsMmSwOrder(String s) {
        int second = CommonUtils.tryParseInt(s, 10);
        Date t = DateTime.now().minusSeconds(second).toDate();
        gpsMmSwOrderService.doSyncGpsMmSwOrder(t, null, false);
    }

    public void syncSupplierNoGpsMmSwOrderHandler(String s) {
        gpsMmSwOrderService.doSyncGpsMmSwOrder(null, s, true);
    }

    public void syncSupplierNoGpsMmSwOrderLabelHandler(String s) {
        gpsMmSwOrderLabelService.doSyncGpsMmSwOrderLebel(null, s);
    }

    public void syncGpsMmSwOrderLabelHandler(String s) {
        int second = CommonUtils.tryParseInt(s, 10);
        Date t = DateTime.now().minusSeconds(second).toDate();
        gpsMmSwOrderLabelService.doSyncGpsMmSwOrderLebel(t, null);
    }

    public void syncGpsMmJisoPoHeadHandler(String s) {
        int second = CommonUtils.tryParseInt(s, 10);
        Date t = DateTime.now().minusSeconds(second).toDate();
        gpsMmJisoPoHeadService.syncGpsMmJisoPoHeadHandler(t, null, false);
    }

    public void syncSupplierGpsMmJisoPoHeadHandler(String s) {
        gpsMmJisoPoHeadService.syncGpsMmJisoPoHeadHandler(null, s, true);
    }
}
